<?php
namespace App\Http\Resources\Traits;

trait FilterResource
{
    public static $only = [];
    public static $except = [];

    public function show($index): bool
    {
        if (!empty(self::$only)) {
            return in_array($index, self::$only);
        }

        if (!empty(self::$except)) {
            return !in_array($index, self::$except);
        }

        return true;
    }

    /**
     * @return $this
     */
    public function only($only = []): FilterResource
    {
        self::$only = $only;
        return $this;
    }

    /**
     * @return $this
     */
    public function except($except = []): FilterResource
    {
        self::$except = $except;
        return $this;
    }

    public function map(): array
    {
        return [];
    }

    public function extra(): array
    {
        $return = [];
        foreach ($this->map() as $index => $item) {
            $return[$index] = $this->when($this->show($index), $item);
        }
        return $return;
    }
}
